package com.digitalchina.lxnsy.viewholder;

import android.app.Activity;
import androidx.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.constant.Constant;
import com.digitalchina.lxnsy.entity.MyOrderListVO;
import com.digitalchina.lxnsy.entity.PayVO;
import com.digitalchina.lxnsy.interfaces.IOrderCallBack;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.ToastUtil;
import com.digitalchina.lxnsy.wxapi.WXPayEntryActivity;
import com.facebook.drawee.view.SimpleDraweeView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.viewholder
 * 创建人：郑皓
 * 创建时间：2017年7月27日 19:54
 * 修改备注：
 * ================================================
 */

public class OrderItemHolder extends BaseViewHolder<MyOrderListVO> {

    // 商家logo
    private SimpleDraweeView sdvLogo;
    // 商家名称
    private TextView tvFarmName;
    // 商家电话
    private TextView tvTel;
    // 订单状态
    private TextView tvStatus;
    // 商品容器
    private LinearLayout layout;
    // 商品数量
    private TextView tvNum;
    // 商品总价
    private TextView tvPrice;
    // 邮费
    private TextView tvFreight;
    // 左边按钮
    private TextView tvLeft;
    // 右边按钮
    private TextView tvRight;

    private IOrderCallBack iOrderCallBack;


    public OrderItemHolder(ViewGroup parent, IOrderCallBack iOrderCallBack) {
        super(parent, R.layout.item_my_order);
        this.iOrderCallBack = iOrderCallBack;
        sdvLogo = $(R.id.item_my_order_iv_farm_logo);
        tvFarmName = $(R.id.item_my_order_tv_farm_name);
        tvTel = $(R.id.item_my_order_tv_farm_tel);
        tvStatus = $(R.id.item_my_order_tv_order_status);
        layout = $(R.id.item_my_order_linearLayout);
        tvNum = $(R.id.item_my_order_tv_product_number);
        tvPrice = $(R.id.item_my_order_tv_order_price);
        tvFreight = $(R.id.item_my_order_tv_order_freight);
        tvLeft = $(R.id.item_my_order_tv_button_left);
        tvRight = $(R.id.item_my_order_tv_button_right);

    }

    @Override
    public void setData(final MyOrderListVO data) {
        super.setData(data);

        FrescoUtil.showImageSmall(data.getSellerLogo(), sdvLogo);

        tvFarmName.setText(data.getSellerName());
        if (StringUtil.isStrEmpty(data.getSellerPhone())){
            tvTel.setVisibility(View.GONE);
        }else{
            tvTel.setVisibility(View.VISIBLE);
            tvTel.setText(data.getSellerPhone());
        }

        tvStatus.setText(data.getEqualStatusDesc());
        tvNum.setText(data.getOrderItems() != null ? data.getOrderItems().size() + "" : "1");
        tvPrice.setText(data.getTotalPriceReal() + "");
        tvFreight.setText("（含运费" + data.getFreightPriceOrigin() + "元）");


        tvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        layout.removeAllViews();
        for (int i = 0; i < data.getOrderItems().size(); i++) {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.view_my_order_product, null);
            SimpleDraweeView simpleDraweeView = (SimpleDraweeView) view.findViewById(R.id.view_my_order_product_iv_picture);
            TextView tvTitle = (TextView) view.findViewById(R.id.view_my_order_product_tv_title);
            TextView tvPrice = (TextView) view.findViewById(R.id.view_my_order_product_tv_price);
            TextView tvNumber = (TextView) view.findViewById(R.id.view_my_order_product_tv_number);
            TextView tvAttrName = (TextView) view.findViewById(R.id.view_my_order_product_tv_attrName);

            FrescoUtil.showImageSmall(data.getOrderItems().get(i).getProductImg(), simpleDraweeView);
            tvTitle.setText(data.getOrderItems().get(i).getProductName());
            tvPrice.setText(data.getOrderItems().get(i).getPrice() + "");
            tvNumber.setText(String.valueOf(data.getOrderItems().get(i).getQuantity()));
            if (StringUtil.isStrEmpty(data.getOrderItems().get(i).getAttrName())) {
                tvAttrName.setVisibility(View.INVISIBLE);
            } else {
                tvAttrName.setVisibility(View.VISIBLE);
                tvAttrName.setText(data.getOrderItems().get(i).getAttrName());
            }


            layout.addView(view);
        }

        switch (data.getEqualStatus()) {
            case Constant.ORDER_STATUS_POST_WAIT_DELIVERY://待发货
                setButtonText("", "取消订单");
                tvRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        cancelOrder(getAdapterPosition());
                    }
                });
                break;
            case Constant.ORDER_STATUS_POST_WAIT_RECEIVING://待收货
                setButtonText("查看物流", "确认收货");
                tvLeft.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iOrderCallBack.onLogisticsClick(getAdapterPosition());
                    }
                });

                tvRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new MaterialDialog.Builder(getContext())
                                .title("提示")
                                .content("是否确认收货？")
                                .positiveText("确定")
                                .negativeText("取消")
                                .onPositive(new MaterialDialog.SingleButtonCallback() {
                                    @Override
                                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                        iOrderCallBack.onAffirmClick(getAdapterPosition());
//                                        BusinessFarmSystem.affirmReceipt(getContext(), data.getOrderNo(), mHandler);
                                    }
                                })
                                .show();
                    }
                });
                break;
            case Constant.ORDER_STATUS_POST_WAIT_EVALUATE://待评价
                setButtonText("删除订单", "评价");
                tvLeft.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        deleteOrder(getAdapterPosition());
                    }
                });
                tvRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iOrderCallBack.onEvaluateClick(getAdapterPosition());
                    }
                });
                break;
            case Constant.ORDER_STATUS_POST_CLOSE://已关闭
                setButtonText("", "删除订单");

                tvRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        deleteOrder(getAdapterPosition());
                    }
                });
                break;
            case Constant.ORDER_STATUS_POST_CANCEL://已取消
                setButtonText("", "删除订单");
                tvRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        deleteOrder(getAdapterPosition());
                    }
                });

                break;
            case Constant.ORDER_STATUS_POST_WAIT_PAYMENT://待支付
                setButtonText("取消订单", "支付");
                tvLeft.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        cancelOrder(getAdapterPosition());
                    }
                });
                tvRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        PayVO payVO = new PayVO();
                        payVO.setPrice(data.getTotalPriceReal());
                        payVO.setOrderNO(data.getOrderNo());
                        payVO.setType(1);
                        GotoUtil.gotoActivity((Activity) getContext(), WXPayEntryActivity.class, "payVO", payVO);
                    }
                });
                break;

        }

    }

    /**
     * 删除订单
     */
    private void deleteOrder(final int pos) {
        new MaterialDialog.Builder(getContext())
                .title("提示")
                .content("是否确认删除该订单？")
                .positiveText("确定")
                .negativeText("取消")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        iOrderCallBack.onDeleteClick(pos);

                    }
                })
                .show();
    }

    /**
     * 取消订单
     */
    private void cancelOrder(final int pos) {
        new MaterialDialog.Builder(getContext())
                .title("提示")
                .content("您确定要取消订单吗？")
                .items(R.array.cancelReason)
                .itemsCallbackSingleChoice(0, new MaterialDialog.ListCallbackSingleChoice() {
                    @Override
                    public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                        if (text == null || text == "") {
                            ToastUtil.showToast(getContext(), "请选择一个理由！");
                            return true;
                        }
                        iOrderCallBack.onCancelClick(pos, text.toString());
                        return true;
                    }
                })
                .positiveText("确定")
                .negativeText("取消")
                .show();
    }

    private void setButtonText(String leftButtonText, String rightButtonText) {
        // 左边按钮
        if (StringUtil.isStrEmpty(leftButtonText))
            tvLeft.setVisibility(View.GONE);
        else {
            tvLeft.setText(leftButtonText);
            tvLeft.setVisibility(View.VISIBLE);
        }

        // 右边按钮
        if (StringUtil.isStrEmpty(rightButtonText))
            tvRight.setVisibility(View.GONE);
        else {
            tvRight.setText(rightButtonText);
            tvRight.setVisibility(View.VISIBLE);
        }
    }

}
